/*
 *      Created on: Mar 4, 2011
 *      Author: topcoder
 */
#include <Rectangle.h>
#include <MathAlgorithm.h>

namespace
{
    const double MIN_SIDE_LENGTH = 0;
    const double MAX_SIDE_LENGTH = 100;
    const double MIN_SIDE_WIDTH = 0;
    const double MAX_SIDE_WIDTH = 75;
}

Rectangle::Rectangle(double length, double width)
{
   width_m = getTruncateValue(width, 2);
   length_m = getTruncateValue(length, 2);;
}

double Rectangle::setLength(double length)
{
    double tempLength = getTruncateValue(length, 2);

    if(isParameterValid(tempLength, MIN_SIDE_LENGTH, MAX_SIDE_LENGTH))
    {
        length_m = tempLength;
    }        
}

double Rectangle::setWidth(double width)
{
    double tempWidth = getTruncateValue(width, 2);

    if(isParameterValid(tempWidth, MIN_SIDE_WIDTH, MAX_SIDE_WIDTH))
    {
        width_m = tempWidth;
    } 
}

bool Rectangle::isShapeValid() const
{
    return (isParameterValid(length_m, MIN_SIDE_LENGTH, MAX_SIDE_LENGTH))
           &&(isParameterValid(width_m, MIN_SIDE_WIDTH, MAX_SIDE_WIDTH));
}

double Rectangle::calculateArea() const
{
    return (isShapeValid())?(getRoundValue(length_m * width_m, 2)):0;
}

double Rectangle::calculatePerimeter() const
{
    return (isShapeValid())?(getRoundValue(2 * (length_m + width_m), 2)):0;
}


